home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Mixed Mode Maddness / Source / main.c
Encoding:
C/C++ Source or Header  |  2000-06-24  |  3.1 KB  |  122 lines

  1. #include <Traps.h>
  2. #include <A4Stuff.h>
  3. #include <LowMem.h>
  4. /*
  5. #include "6502.h"
  6. #include "global.h"
  7.  
  8. #include "interpret.h"
  9. */
  10.  
  11. UniversalProcPtr gOldMixedModeMagic;
  12. Handle gInterpreterCode;
  13.  
  14. asm void MyMixedModeMagicPatch(void);
  15. pascal void    InstallGlobals(void);
  16. Ptr gRom6502 = nil;
  17. SInt32 gCallBack=nil;
  18.  
  19. #define kRegsToSave                    14        // number of registers pushed on the stack
  20.                                             // stack spac registers take up
  21. #define    StackRegSize                (kRegsToSave * sizeof(long))
  22. #define kNEWCPU                        0x03    // ISA / RTA of our FAKE CPU
  23.  
  24. void main(void)
  25. {
  26.     Handle h;                                // Handle to resource
  27.     UniversalProcPtr myPatch;
  28.     OSErr err;
  29.     THz theZone;
  30.  
  31.     EnterCodeResource();                    // Get globals
  32.  
  33.     theZone = GetZone();
  34.     SetZone(SystemZone());
  35.  
  36.     h = GetResource('INIT', 0);                // Get the resource
  37.     HLock(h);
  38.     DetachResource(h);                        // Detach
  39.                                             // Get old Trap
  40.  
  41.     h = GetResource('ROMA', 128);            // Get 6502 ROM
  42.     if (h) {
  43.         HLock(h);
  44.         DetachResource(h);
  45.         gRom6502 = *h;
  46.     }
  47.  
  48.     err = NewGestaltValue('6502', (SInt32)& gCallBack);
  49.     
  50.     gInterpreterCode = GetResource('INTP', 128);
  51.     if (!gInterpreterCode) {
  52.         DebugStr("\pYou bettter do an es");
  53.         HLock(gInterpreterCode);
  54.     }
  55.     DetachResource(gInterpreterCode);
  56.     gInterpreterCode = (Handle) *gInterpreterCode;
  57.  
  58.     gOldMixedModeMagic = NGetTrapAddress(_MixedModeMagic, (_MixedModeMagic & 0x0800) ? ToolTrap : OSTrap);
  59.  
  60.     InstallGlobals();                        // Instal globals for patch
  61.  
  62.     myPatch = NewRoutineDescriptor((ProcPtr)MyMixedModeMagicPatch, 0, GetCurrentArchitecture);
  63.  
  64.                                             // Set new trap
  65.     NSetTrapAddress(myPatch, _MixedModeMagic, (_MixedModeMagic & 0x0800) ? ToolTrap : OSTrap);
  66.  
  67.     SetZone(theZone);
  68.  
  69.     ExitCodeResource();                        // Done fooling around with globals
  70. }
  71.  
  72. asm void MyMixedModeMagicPatch(void)
  73. {
  74.             bra.s    start                    // branch to real start
  75.  
  76.             entry    static InstallGlobals    // entry point to install globals
  77.  
  78. InstallGlobals:
  79.             lea        oldMagic, a0            // get address to save globals
  80.             move.l    gOldMixedModeMagic,(a0)    // save old mixmodeaddress
  81.             lea        interpret, a0
  82.             move.l    gInterpreterCode,(a0)
  83.             lea     gCallBack,a1
  84.             lea     callBack,a0
  85.             move.l    a1,(a0)
  86.             rts                                // bye
  87.  
  88. start:
  89.             movem.l    a0-a6/d0-d6,-(sp)        // save all registers
  90.  
  91.             move.l    StackRegSize(sp),d0        // get stack
  92.             subi.l    #2,d0                    // subtract 2
  93.             movea.l    d0,a0                    // a0 now contains RoutineDescriptorPtr
  94.  
  95.             cmpi.w    #0,10(a0)                // Number of routine descriptors
  96.             bne.s    normalMixedMode            // We only use 1 routine
  97.  
  98.             cmpi.b    #kNEWCPU, 17(a0)        // see if it is our CPU
  99.             bne.s    normalMixedMode            // no
  100.  
  101.             add.l    #0x20,a0                // A0 now points to the code
  102.             move.l    interpret,a2            // a2 points to interpreter
  103.             move.l    callBack,a1                // a1 is a Ptr to a callback function
  104.             move.l    (a1),a1                    // deref
  105.             jsr        (a2)                    // InterPret the code
  106.  
  107.             movem.l    (sp)+,a0-a6/d0-d6        // restore regs
  108.             addq.l    #4,sp                    // This makes it work (Not sure Why?????)
  109.             rts
  110.  
  111. normalMixedMode:
  112.             movem.l    (sp)+,a0-a6/d0-d6        // normal MixedModeMagic
  113.             move.l    oldMagic,-(sp)            // put original trap address on stack
  114.             rts                                // Bye!
  115.  
  116. oldMagic:    dc.l    0                        // storage for globals
  117. result:     dc.l    0
  118. interpret:    dc.l    0
  119. callBack:    dc.l    0
  120.  
  121. }
  122.